package cn.edu.bjfu.practice.practice.teacher.controller;

import cn.edu.bjfu.practice.practice.app.common.response.ErrorFromUser;
import cn.edu.bjfu.practice.practice.app.common.response.Response;
import cn.edu.bjfu.practice.practice.teacher.pojo.query.AddCaseForm;
import cn.edu.bjfu.practice.practice.teacher.pojo.query.AddCourseForm;
import cn.edu.bjfu.practice.practice.teacher.pojo.query.AddOneResForm;
import cn.edu.bjfu.practice.practice.teacher.pojo.query.ModifyCaseForm;
import cn.edu.bjfu.practice.practice.teacher.service.CaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
public class CaseController {

    @Autowired
    CaseService caseService;

    @PostMapping("/teacher/add_case")
    public Response<Object> addCase(AddCaseForm form) {
        try {
            return Response.success(caseService.addCase(form));
        }catch (IOException e) {
            return Response.message(e.getLocalizedMessage());
        }
    }

    @PostMapping("/teacher/select_case")
    public Response<Object> selectCase(Integer id) {
        return Response.success(caseService.selectCase(id));
    }

    @PostMapping("/teacher/get_case")
    public Response<Object> getCase(Integer id) {
        return Response.success(caseService.getCase(id));
    }

    @PostMapping("/teacher/modify_case")
    public Response<Object> modifyCase(ModifyCaseForm form) {
        caseService.modifyCase(form);
        return Response.message();
    }

    @PostMapping("/teacher/delete_case")
    public Response<Object> deleteCase(Integer id) {
        caseService.deleteCase(id);
        return Response.message();
    }

    @PostMapping("/teacher/add_resource")
    public Response<Object> addResource(AddOneResForm form) {
        if(caseService.addResource(form) == 1)
            return Response.message();
        else
            return Response.error(ErrorFromUser.FILE_READ_ERROR);
    }

    @PostMapping("/teacher/delete_resource")
    public Response<Object> deleteResource(Integer id) {
        caseService.deleteResource(id);
        return Response.message();
    }
}
